//
//  DeliveryVc.m
//  Wy_BuyBooks
//
//  Created by wangyang on 2020/2/6.
//  Copyright © 2020 wangyang. All rights reserved.
//

#import "DeliveryVc.h"
#import "DeliveryVcCell.h"
#import "WyLnadViewController.h"
@interface DeliveryVc ()<UITableViewDelegate,UITableViewDataSource,DeliveryVcCellDele>

@property (nonatomic,strong) UITableView     *WyHomeTableView;
@property (nonatomic,strong) NSMutableArray     *WyOrderVcArray;
@property (nonatomic,strong) NSArray           *WyOrderVcarray;

@end

@implementation DeliveryVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initSetArray];
    
    self.view.backgroundColor = WyWhiteColor;
     [self WyHomeTableView];
}
-(void)initSetArray
{


    _WyOrderVcArray = [[NSMutableArray alloc]init];

          NSMutableArray  *Order = [[NSUserDefaults standardUserDefaults]objectForKey:@"DeliveryVclist"];

          if (Order == nil && Order.count == 0) {
              _WyOrderVcarray = @[@{@"order":@"订单编号：189128912",
                                    @"img":@"鲁迅小说全集",
                                    @"name":@"鲁迅小说全集",
                                    @"UserName":@"鲁迅",
                                    @"Score":@"综合评分：4.6",
                                    @"money":@"¥66.00",
                                    @"all":@"共1件商品  合计：¥99.00",
                                    @"people":@"收货人：李志军",
                                    @"phpne":@"17990281721",
                                    @"add":@"收货地址：河南省郑州市中原区大学科技园东区8号楼1209",
                                    @"time":@"送达时间：2019-09-09 12:00"},
                                  ];
              NSMutableArray  *nsmArray = [[NSMutableArray alloc]init];
              for (int i = 0; i<_WyOrderVcarray.count; i++) {
                  [nsmArray addObject:_WyOrderVcarray[i]];
              }
              [[NSUserDefaults standardUserDefaults]setValue:nsmArray forKey:@"DeliveryVclist"];
          }
          NSMutableArray *nsm = [[NSUserDefaults standardUserDefaults]objectForKey:@"DeliveryVclist"];
          [_WyOrderVcArray removeAllObjects];
          for (int i=0; i<nsm.count; i++) {
              [_WyOrderVcArray addObject:nsm[i]];
          }

}


-(UITableView *)WyHomeTableView

{
    
    if (!_WyHomeTableView) {
        
        
        _WyHomeTableView = [[UITableView alloc]init];
           _WyHomeTableView.delegate = self;
           _WyHomeTableView.dataSource = self;
           _WyHomeTableView.showsVerticalScrollIndicator = YES;
           _WyHomeTableView.backgroundColor = [UIColor clearColor];
           _WyHomeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
           _WyHomeTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(Shuaxin)];
           [self.view addSubview:_WyHomeTableView];
           [_WyHomeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
               make.left.right.top.bottom.mas_offset(0);
           }];
           [_WyHomeTableView.mj_header beginRefreshing];
           [_WyHomeTableView registerClass:[DeliveryVcCell class] forCellReuseIdentifier:@"Cell"];
    }
    return _WyHomeTableView;
    
}
-(void)Shuaxin
{
    [self initSetArray];
    [self.WyHomeTableView reloadData];
    [self.WyHomeTableView.mj_header endRefreshing];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{
    return _WyOrderVcArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *CellId = @"Cell";
    
    DeliveryVcCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
    
    if (!cell) {
        cell = [[DeliveryVcCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    cell.IndexPath = indexPath;
    cell.PicImg.image = [UIImage imageNamed:_WyOrderVcArray[indexPath.row][@"img"]];
    cell.OrderLabe.text = _WyOrderVcArray[indexPath.row][@"order"];
    cell.NameLabe.text = _WyOrderVcArray[indexPath.row][@"name"];
    cell.ScoreLabe.text = _WyOrderVcArray[indexPath.row][@"Monthly"];
    cell.MoneyLabe.text = _WyOrderVcArray[indexPath.row][@"money"];
    cell.UserNameLabe.text = _WyOrderVcArray[indexPath.row][@"order"];
    
    return cell;
}


-(void)ContactMerchantClickIndexPath:(NSIndexPath *)IndexPath
{
    
    if ([WyEngineering sharedUser].login == YES) {

                  [MyProgressHUD showWithMaskType:MyProgressHUDMaskTypeBlack];

                        //定时器
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{

                          [MyProgressHUD dismiss];
                          [[AlertControllerCustom shareInstance] showAlert:@"17990281721" viewController:self];
            
                        });


              }else{

                  [MyProgressHUD showWithMaskType:MyProgressHUDMaskTypeBlack];

                               //定时器
                               dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{

                                 [MyProgressHUD dismiss];
                                WyLnadViewController *VC = [[WyLnadViewController alloc] init];
                                                    VC.LandVcBlock = ^{
                                                        //                        [self.MyTableView removeFromSuperview];
                                                        //                        self.MyTableView = nil;
                                                        self.tabBarController.selectedIndex = 0;
                                                    };
                                                    [self.navigationController pushViewController:VC animated:YES];
          //                         [self presentViewController:VC animated:YES completion:nil];
                               });


              }

    
   
}

-(void)CancelOrderClickIndexPath:(NSIndexPath *)IndexPath

{
    if ([WyEngineering sharedUser].login == YES) {

            [MyProgressHUD showWithMaskType:MyProgressHUDMaskTypeBlack];

                  //定时器
                  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                     
                    [MyProgressHUD dismiss];
                   
        if (IndexPath.row<self.WyOrderVcArray.count) {
                           
                           
                           self.WyOrderVcArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"DeliveryVclist"]];
                           
                           NSArray *yiwancheng = [[NSUserDefaults standardUserDefaults] objectForKey:@"CompletedVclist"];
                           NSMutableArray *arraym = [NSMutableArray arrayWithArray:yiwancheng];
                           [arraym addObject:self.WyOrderVcArray[IndexPath.row]];
                           
                           [[NSUserDefaults standardUserDefaults] setValue:arraym forKey:@"CompletedVclist"];
                           [self.WyOrderVcArray removeObjectAtIndex:IndexPath.row];
                           
                           
                           [[NSUserDefaults standardUserDefaults] setValue:self.WyOrderVcArray forKey:@"DeliveryVclist"];
                           
                           [self.WyHomeTableView reloadData];
                           
                           if (@available(iOS 10.0, *)) {
                               [self.WyHomeTableView refreshControl];
                           } else {
                               // Fallback on earlier versions
                           }
             }
           
        [[AlertControllerCustom shareInstance] showAlert:@"订单已确定" viewController:self];
                  });
            
            
        }else{
            
            [MyProgressHUD showWithMaskType:MyProgressHUDMaskTypeBlack];

                         //定时器
                         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                            
                           [MyProgressHUD dismiss];
                          WyLnadViewController *VC = [[WyLnadViewController alloc] init];
                                              VC.LandVcBlock = ^{
                                                  //                        [self.MyTableView removeFromSuperview];
                                                  //                        self.MyTableView = nil;
                                                  self.tabBarController.selectedIndex = 0;
                                              };
                                              [self.navigationController pushViewController:VC animated:YES];
    //                         [self presentViewController:VC animated:YES completion:nil];
                         });
        
           
        }
    [MyProgressHUD showWithMaskType:MyProgressHUDMaskTypeBlack];
          
          
   
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return BiLiWidth(225);
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
